<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">


<meta name="google-site-verification" content="E9deYnivN5MuHMuIfiMZZfS0alv-d_0UjcwjBL79lGU" />



<meta name="baidu-site-verification" content="iHYWJxscwD" />










<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="true" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Python,量化投资,编程难题,MIT,进制运算," />










<meta name="description" content="《programming for the puzzled》第五章涉及到的知识：break语句，基数运算(radix representations)问题概述：测量一些水晶球的“硬度系数”。2015年完工的上海塔有128层，你要找出从多高的地方扔下这些水晶球，它们不会破碎而是弹起来。返回的结果是满足条件的最高的楼层数。比如你返回的是f，则从f层扔水晶不会破碎，从f+1层扔就会了。水晶球一旦破碎，就不">
<meta property="og:type" content="article">
<meta property="og:title" content="量化投资学习笔记50——通过问题学算法:05请打碎那颗水晶(Please Do Break the Crystal)">
<meta property="og:url" content="https://zwdnet.github.io/2020/05/10/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B050%E2%80%94%E2%80%94%E9%80%9A%E8%BF%87%E9%97%AE%E9%A2%98%E5%AD%A6%E7%AE%97%E6%B3%95-05%E8%AF%B7%E6%89%93%E7%A2%8E%E9%82%A3%E9%A2%97%E6%B0%B4%E6%99%B6-Please-Do-Break-the-Crystal/index.html">
<meta property="og:site_name" content="赵瑜敏的口腔医学专业学习博客">
<meta property="og:description" content="《programming for the puzzled》第五章涉及到的知识：break语句，基数运算(radix representations)问题概述：测量一些水晶球的“硬度系数”。2015年完工的上海塔有128层，你要找出从多高的地方扔下这些水晶球，它们不会破碎而是弹起来。返回的结果是满足条件的最高的楼层数。比如你返回的是f，则从f层扔水晶不会破碎，从f+1层扔就会了。水晶球一旦破碎，就不">
<meta property="og:locale">
<meta property="og:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">
<meta property="article:published_time" content="2020-05-10T05:19:52.000Z">
<meta property="article:modified_time" content="2020-08-30T05:52:04.000Z">
<meta property="article:author" content="赵瑜敏">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="量化投资">
<meta property="article:tag" content="编程难题">
<meta property="article:tag" content="MIT">
<meta property="article:tag" content="进制运算">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zwdnet.github.io/2020/05/10/量化投资学习笔记50——通过问题学算法-05请打碎那颗水晶-Please-Do-Break-the-Crystal/"/>





  <title>量化投资学习笔记50——通过问题学算法:05请打碎那颗水晶(Please Do Break the Crystal) | 赵瑜敏的口腔医学专业学习博客</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">赵瑜敏的口腔医学专业学习博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zwdnet.github.io/2020/05/10/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B050%E2%80%94%E2%80%94%E9%80%9A%E8%BF%87%E9%97%AE%E9%A2%98%E5%AD%A6%E7%AE%97%E6%B3%95-05%E8%AF%B7%E6%89%93%E7%A2%8E%E9%82%A3%E9%A2%97%E6%B0%B4%E6%99%B6-Please-Do-Break-the-Crystal/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="赵瑜敏的口腔医学专业学习博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">量化投资学习笔记50——通过问题学算法:05请打碎那颗水晶(Please Do Break the Crystal)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-05-10T05:19:52+00:00">
                2020-05-10
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" itemprop="url" rel="index">
                    <span itemprop="name">量化投资</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.5k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  6
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>《programming for the puzzled》第五章<br>涉及到的知识：break语句，基数运算(radix representations)<br>问题概述：测量一些水晶球的“硬度系数”。2015年完工的上海塔有128层，你要找出从多高的地方扔下这些水晶球，它们不会破碎而是弹起来。返回的结果是满足条件的最高的楼层数。比如你返回的是f，则从f层扔水晶不会破碎，从f+1层扔就会了。水晶球一旦破碎，就不能重复使用。球的速度是决定其是否破碎的唯一因素，随着层数的上升，速度增加。你可以假设当球从x层扔出时没有破碎，那它从小于x的楼层扔出也不会破碎。而从大于x的楼层扔出则肯定会破碎。但你不能坐电梯，因此你希望能最小化扔球的次数。如果只有一个球，从第一层扔着来，如果在n层它碎了，你报告的就是n-1，可能的次数是128次。如果是两个球，第一个在128层扔，碎了，第二个从第一层扔，一直可能到127层，需要扔的次数还是128。一个改进是从中间64层开始扔，碎了，从下扔，没碎，从上扔。最差的情况是扔64次。对于2个球，你能想到办法最大化你的报酬，并在21次尝试之前结束吗？如果你有更多水晶球？如果上海塔突然高度增加了一倍呢？<br>我自己想的结果:就是二分法查找嘛，但是只用两个球，确实想不到比64次更少的尝试方法。看看作者怎么说。<br>考虑从20层开始扔，碎了，从1到19层扔，总的次数最坏是20。没碎，可以从40层扔，如此重复。能否找到一个与楼层数n相关的函数func(n)，代表扔的最多少的次数。对于最坏的情况，球一直不破直到最后一次扔，第一个球要在第k,2k,3k,…,(n/k-1)k, (n/k)k层扔出，第一个球要扔n/k次，第二个球要扔k-1次，即总的扔的次数为n/k+k-1次。因此要找到一个k使得n/k+k-1最小。（用求导算一下，f(k) = n/k+k-1, f’(k) = -n/k² +1，当f’(k) = 0时，k = sqrt(n)，书上没有这些。）因此当k为n的平方根时取最小值，对于128层，当k=11时扔的次数最小，最坏的情况下要扔21次。<br>现在来考虑水晶球数量增加的情况。作者的做法是有d个球就用d位数字来代表，进制r则根据使r^d &gt; n的最小的r来选择。例如对于d = 4个球，3^4 = 81 &lt; 128, 4^4 = 256 &gt; 128，所以r = 4。在1000（四进制）层扔第一个，假设其在第2000（四进制）碎了，扔第二个球从1100（四进制，在十进制中为80层）开始扔，以此类推，搜索范围逐渐变小。扔的最少次数为d(r-1)。<br>写一个程序，实现上述算法，对于给定的n和d，给出从哪层开始扔，扔的次数最少。然后，根据扔的结果（碎或不碎），程序告诉我们要扔的新的楼层（没碎），或者告诉我们结果（碎了）。最后还要告诉我们扔的总次数。<br>分析的好复杂啊，尤其用进制那个。直接看作者写的代码吧。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="comment"># 《programming for the puzzled》实操</span></span><br><span class="line"><span class="comment"># 5.打碎水晶</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">howHardIsTheCrystal</span>(<span class="params">n, d</span>):</span></span><br><span class="line">    r = <span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> (r**d &lt; n):</span><br><span class="line">        r += <span class="number">1</span></span><br><span class="line">    print(<span class="string">&quot;选择的基数为&quot;</span>, r)</span><br><span class="line">    numDrops = <span class="number">0</span></span><br><span class="line">    floorNoBreak = [<span class="number">0</span>]*d</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(d):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(r-<span class="number">1</span>):</span><br><span class="line">            floorNoBreak[i] += <span class="number">1</span></span><br><span class="line">            Floor = convertToDecimal(r, d, floorNoBreak)</span><br><span class="line">            <span class="keyword">if</span> Floor &gt; n:</span><br><span class="line">                floorNoBreak[i] -= <span class="number">1</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            print(<span class="string">&quot;从&quot;</span>, Floor, <span class="string">&quot;层扔下第&quot;</span>, i+<span class="number">1</span>, <span class="string">&quot;个球。&quot;</span>)</span><br><span class="line">            yes = <span class="built_in">input</span>(<span class="string">&quot;水晶球裂了吗?(yes/no):&quot;</span>)</span><br><span class="line">            numDrops += <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span> yes == <span class="string">&quot;yes&quot;</span>:</span><br><span class="line">                floorNoBreak[i] -= <span class="number">1</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">                </span><br><span class="line">    hardness = convertToDecimal(r, d, floorNoBreak)</span><br><span class="line">    print(<span class="string">&quot;硬度为:&quot;</span>, hardness)</span><br><span class="line">    print(<span class="string">&quot;扔球的总数为:&quot;</span>, numDrops)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">convertToDecimal</span>(<span class="params">r, d, rep</span>):</span></span><br><span class="line">    number = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(d-<span class="number">1</span>):</span><br><span class="line">        number = (number + rep[i])*r</span><br><span class="line">    number += rep[d-<span class="number">1</span>]</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> number</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    howHardIsTheCrystal(<span class="number">128</span>, <span class="number">4</span>)</span><br></pre></td></tr></table></figure>
<p>练习1到练习3，都是对代码的改动或增加新功能，直接放程序了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding:utf-8</span></span><br><span class="line"><span class="comment"># 《programming for the puzzled》实操</span></span><br><span class="line"><span class="comment"># 5.打碎水晶</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">howHardIsTheCrystal</span>(<span class="params">n, d</span>):</span></span><br><span class="line">    r = <span class="number">1</span></span><br><span class="line">    <span class="keyword">while</span> (r**d &lt; n):</span><br><span class="line">        r += <span class="number">1</span></span><br><span class="line">    print(<span class="string">&quot;选择的基数为&quot;</span>, r)</span><br><span class="line">    <span class="comment"># 练习1，有时d太大，会跳过第一个球</span></span><br><span class="line">    <span class="comment"># 减少d的值</span></span><br><span class="line">    newd = d</span><br><span class="line">    <span class="keyword">while</span> (r**(newd-<span class="number">1</span>) &gt; n):</span><br><span class="line">        newd -= <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> newd &lt; d:</span><br><span class="line">        print(<span class="string">&quot;只用了&quot;</span>, newd, <span class="string">&quot;个球&quot;</span>)</span><br><span class="line">    d = newd</span><br><span class="line">   </span><br><span class="line">    numDrops = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 练习2 输出坏了的球</span></span><br><span class="line">    numBreaks = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 练习3，输出正在考虑的楼层区间</span></span><br><span class="line">    start = <span class="number">0</span></span><br><span class="line">    end = n</span><br><span class="line">    floorNoBreak = [<span class="number">0</span>]*d</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(d):</span><br><span class="line">        <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(r-<span class="number">1</span>):</span><br><span class="line">            <span class="comment"># 练习3，输出正在考虑的区间</span></span><br><span class="line">            print(<span class="string">&quot;正在考虑&quot;</span>, start, <span class="string">&quot;到&quot;</span>, end, <span class="string">&quot;的楼层。&quot;</span>)</span><br><span class="line">            floorNoBreak[i] += <span class="number">1</span></span><br><span class="line">            Floor = convertToDecimal(r,  d, floorNoBreak)</span><br><span class="line">            <span class="keyword">if</span> Floor &gt; n:</span><br><span class="line">                floorNoBreak[i] -= <span class="number">1</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            print(<span class="string">&quot;从&quot;</span>, Floor, <span class="string">&quot;层扔下第&quot;</span>, i+<span class="number">1</span>, <span class="string">&quot;个球。&quot;</span>)</span><br><span class="line">            yes = <span class="built_in">input</span>(<span class="string">&quot;水晶球裂了吗?(yes/no):&quot;</span>)</span><br><span class="line">            numDrops += <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span> yes == <span class="string">&quot;yes&quot;</span>:</span><br><span class="line">                floorNoBreak[i] -= <span class="number">1</span></span><br><span class="line">                end = Floor-<span class="number">1</span></span><br><span class="line">                <span class="keyword">break</span></span><br><span class="line">            <span class="comment"># 练习2</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                numBreaks += <span class="number">1</span></span><br><span class="line">                start = Floor+<span class="number">1</span></span><br><span class="line">               </span><br><span class="line">    hardness = convertToDecimal(r,  d, floorNoBreak)</span><br><span class="line">    print(<span class="string">&quot;硬度为:&quot;</span>, hardness)</span><br><span class="line">    print(<span class="string">&quot;扔球的总数为:&quot;</span>, numDrops)</span><br><span class="line">    <span class="comment"># 练习2</span></span><br><span class="line">    print(<span class="string">&quot;扔坏了的球的个数为:&quot;</span>, numBreaks)</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">   </span><br><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">convertToDecimal</span>(<span class="params">r,  d, rep</span>):</span></span><br><span class="line">    number = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(d-<span class="number">1</span>):</span><br><span class="line">        number = (number + rep[i])*r</span><br><span class="line">    number += rep[d-<span class="number">1</span>]</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">return</span> number</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    howHardIsTheCrystal(<span class="number">128</span>, <span class="number">6</span>)</span><br></pre></td></tr></table></figure>
<p>这章的问题感觉比较奇怪，怎么想到用不同进制来代表不同的球数的?看看视频吧。</p>
<p>我发文章的三个地方，欢迎大家在朋友圈等地方分享，欢迎点“在看”。<br>我的个人博客地址：<a href="https://zwdnet.github.io/">https://zwdnet.github.io</a><br>我的知乎文章地址： <a target="_blank" rel="noopener" href="https://www.zhihu.com/people/zhao-you-min/posts">https://www.zhihu.com/people/zhao-you-min/posts</a><br>我的微信个人订阅号：赵瑜敏的口腔医学学习园地</p>
<p><img src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/wx.jpg"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欢迎打赏！感谢支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/mm_facetoface_collect_qrcode_1542944836634.png" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/1542944857770.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
            <a href="/tags/%E9%87%8F%E5%8C%96%E6%8A%95%E8%B5%84/" rel="tag"># 量化投资</a>
          
            <a href="/tags/%E7%BC%96%E7%A8%8B%E9%9A%BE%E9%A2%98/" rel="tag"># 编程难题</a>
          
            <a href="/tags/MIT/" rel="tag"># MIT</a>
          
            <a href="/tags/%E8%BF%9B%E5%88%B6%E8%BF%90%E7%AE%97/" rel="tag"># 进制运算</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/05/09/%E4%BD%A0%E4%BC%9A%E5%A4%87%E7%89%99%E5%90%97%E2%80%94%E2%80%94%E5%89%8D%E7%89%99%E9%A2%84%E5%A4%87%E5%AE%8C%E6%88%90/" rel="next" title="你会备牙吗——前牙预备完成">
                <i class="fa fa-chevron-left"></i> 你会备牙吗——前牙预备完成
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/05/11/%E7%89%99%E9%AB%93%E7%97%85%E6%80%BB%E7%BB%9318%E2%80%94%E2%80%94%E7%89%99%E9%AB%93%E5%86%8D%E7%94%9F/" rel="prev" title="牙髓病总结18——牙髓再生">
                牙髓病总结18——牙髓再生 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MTA2Mi8xNzU4Nw=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://zymblog-1258069789.cos.ap-chengdu.myqcloud.com/other/tx.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">452</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">544</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">本站版权归赵瑜敏所有，如欲转载请与本人联系。</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">1225.8k</span>
  
</div>









<div>
  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1275447216'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1275447216%26online%3D1%26show%3Dline' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1275447216&web_id=1275447216" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  

  

  
  

  

  

  

  
</body>
</html>
